package merchant.space;

import android.app.Dialog;
import android.app.ExpandableListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;

public class BrowseCollectionsActivity extends ExpandableListActivity {

	ExpandableListAdapter mAdapter;
	String cattegory;
	private int pos;
	private Collections collections;
	
	private static final int LOADING = 0;
	private ProgressDialog progressdialog = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		collections = DataLink.getBrowsedCollections();
		mAdapter = new BrowseCollectionsAdapter(this, collections);
		
		setListAdapter(mAdapter);
		
		registerForContextMenu(getExpandableListView());
		Log.d("CREATING","COLLECTION");
		
		
	}
	
	@Override
	public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id)
	{
		showDialog(LOADING);
		
		new Thread(){ public void run() {
			
			DataLink.prepareBrowsedCollection(pos);
			threadHandler.sendEmptyMessage(0);
			  }}.start();
		
		
		return true;
		
	}
	
	
	
	
	public void viewCollection()
	{
		
		Intent viewcoll = new Intent(getParent(), ViewCollectionActivity.class);
		BrowseGroupActivity parentActivity = (BrowseGroupActivity) getParent();
		parentActivity.startChildActivity("ViewCollectionActivity", viewcoll);
		
	}
	

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case LOADING:
			//Log.d("HMM",getParent().toString());
			progressdialog = new ProgressDialog(getParent());
			progressdialog.setTitle("Merchant");
			progressdialog.setMessage("Loading data");
			progressdialog.setCancelable(false);
			return progressdialog;
		
		default:
		
			 Log.d("aaah","FUCK");
		     return null;
		}
		
	}
	
	private final Handler threadHandler = new Handler() {
		@Override
		public void handleMessage(Message arg0) {
			switch(arg0.what)
			{
				
				case 0:
				{
					dismissDialog(LOADING);
					viewCollection();
				} break;
				
				default:
				{
					Log.d("DEF","ault");
				} break;
			}
		}};
	
	
}